home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / comm2 / termsorc.lha / Extras / Source / gtlayout-source.lha / LTP_ConvertNum.c < prev    next >
C/C++ Source or Header  |  1995-09-24  |  1KB  |  97 lines

  1. /*  GadTools layout toolkit
  2. **
  3. **  Copyright © 1993-1995 by Olaf `Olsen' Barthel
  4. **  Freely distributable.
  5. */
  6.  
  7. #include "gtlayout_global.h"
  8.  
  9. #ifdef DO_HEXHOOK
  10.  
  11.     // Check whether the buffer only contains a valid hex/binary/octal number...
  12.  
  13. BOOLEAN __regargs
  14. LTP_ConvertNum(BOOLEAN negAllowed,STRPTR buffer,LONG *value)
  15. {
  16.     UBYTE    ch;
  17.     ULONG    num;
  18.     LONG    neg;
  19.  
  20.     num = 0;
  21.  
  22.     if((buffer[0] == '-') && negAllowed)
  23.     {
  24.         neg = -1;
  25.  
  26.         buffer++;
  27.     }
  28.     else
  29.         neg = 1;
  30.  
  31.     if(((buffer[0] == '0') && ToUpper(buffer[1] == 'X')) || (buffer[0] == '$'))
  32.     {
  33.         if(*buffer++ != '$')
  34.             buffer++;
  35.  
  36.         while(ch = ToUpper(*buffer++))
  37.         {
  38.             num = num * 16;
  39.  
  40.             if((ch >= 'A') && (ch <= 'F'))
  41.                 num += (ch & 0xF) + 9;
  42.             else
  43.             {
  44.                 if ((ch >= '0') && (ch <= '9'))
  45.                     num += ch & 0xF;
  46.                 else
  47.                     return(FALSE);
  48.             }
  49.         }
  50.     }
  51.     else
  52.     {
  53.         if(*buffer == '%')
  54.         {
  55.             buffer++;
  56.  
  57.             while(ch = *buffer++)
  58.             {
  59.                 if((ch < '0') || (ch > '1'))
  60.                     return(FALSE);
  61.  
  62.                 num = (num * 2) + (ch & 0xF);
  63.             }
  64.         }
  65.         else
  66.         {
  67.             if(*buffer == '&')
  68.             {
  69.                 buffer++;
  70.  
  71.                 while(ch = *buffer++)
  72.                 {
  73.                     if((ch < '0') || (ch > '7'))
  74.                         return(FALSE);
  75.  
  76.                     num = (num * 8) + (ch & 0xF);
  77.                 }
  78.             }
  79.             else
  80.             {
  81.                 while(ch = *buffer++)
  82.                 {
  83.                     if((ch < '0') || (ch > '9'))
  84.                         return(FALSE);
  85.  
  86.                     num = (num * 10) + (ch & 0xF);
  87.                 }
  88.             }
  89.         }
  90.     }
  91.  
  92.     *value = num * neg;
  93.  
  94.     return(TRUE);
  95. }
  96. #endif
  97.